<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 发送 AJAX请求</title>
    <!-- <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>这个链接是国外的，跑起来比较慢，下面的是国内的 -->
    <!-- crossorigin="anonymous"  script属性，跨源的请求的属性设置，向当前资源发送请求时不会携带当前域名的cookie -->
    <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>

<body>
    <button>GET</button>
    <button>POST</button>
    <button>AJAX</button>

    <script>
        const btns = document.querySelectorAll('button');

        //配置 baseURL
        axios.defaults.baseURL = 'http://127.0.0.1:8000';

        btns[0].onclick = function () {
            //GET请求
            axios.get('/axios-server', {
                //url 参数
                params: {
                    id: 100,
                    vip: 7
                },
                //请求头信息
                headers: {
                    name: 'liang',
                    age: 23
                }
            }).then(value => {
                console.log(value);
            });
        }

        btns[1].onclick = function () {
            axios.post('/axios-server', {
                //请求体
                username: 'admin',
                password: 'admin'
            }, {
                //其他配置
                //url 参数
                params: {
                    id: 200,
                    vip: 9
                },
                //请求头参数
                headers: {
                    height: 180,
                    weight: 160
                },
            });
        }

        btns[2].onclick = function () {
            axios({
                //请求方法
                method: 'POST',
                //url
                url: '/axios-server',
                //url参数
                params: {
                    id: 666,
                    vip: 10
                },
                //头信息
                headers: {
                    a: 100,
                    b: 200
                },
                //请求体参数
                data: {
                    username: 'admin',
                    password: '123456'
                }
            }).then(Response => {
                // console.log(Response);
                //响应状态码
                console.log(Response.status);
                //响应状态字符串
                console.log(Response.statusText);
                //响应头信息
                console.log(Response.headers);
                //响应体
                console.log(Response.data);
            })
        }
    </script>
</body>

</html>